﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Card.Web.Member
{
    public partial class Account : MemberBase
    {
        #region 属性
        
        #endregion

        #region 事件
        protected override void OnInit(EventArgs e)
        {
            this.btnSave.Click += new EventHandler(btnSave_Click);
            base.OnInit(e);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            if (this.DoCheck())
            {
                Entity.Users user = BLL.UsersManager.GetEntity(this.Passport.UserID, this.Passport);
                user.Sex = !this.rdMen.Checked;
                if (!string.IsNullOrEmpty(this.txtBirthday.Text) && Utility.ValidationHelper.IsDateTime(this.txtBirthday.Text))
                {
                    user.Birthday = DateTime.Parse(this.txtBirthday.Text);
                }
                else
                {
                    user.Birthday = DateTime.MinValue;
                }
                user.Email = this.txtMail.Text;
                user.Qq = this.txtQQ.Text;
                user.Mobile = this.txtTel.Text;
                if (BLL.UsersManager.Update(user, this.Passport))
                {
                    this.MessageBox("修改成功！");
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.DoInit();
            }
        } 
        #endregion

        #region 方法
        private void DoInit()
        {
            Entity.Users user = BLL.UsersManager.GetEntity(this.Passport.UserID, this.Passport);
            if (user != null)
            {
                this.ltlUName.Text = user.UserName;
                this.rdMen.Checked = !user.Sex;
                this.rdGirl.Checked = user.Sex;
                this.txtBirthday.Text = Utility.DateTimeUtility.DateTimeToString(user.Birthday);
                this.txtMail.Text = user.Email;
                this.txtQQ.Text = user.Qq;
                this.txtTel.Text = user.Mobile;
            }
        }

        private bool DoCheck()
        {
            if (!string.IsNullOrEmpty(this.txtBirthday.Text) && !Utility.ValidationHelper.IsDateTime(this.txtBirthday.Text))
            {
                this.MessageBox("请填写正确的出生日期格式！");
                return false;
            }

            if(!string.IsNullOrEmpty(this.txtMail.Text) && !Utility.ValidationHelper.IsEmail(this.txtMail.Text))
            {
                this.MessageBox("邮件格式错误！");
                return false;
            }
            return true;
        }
        #endregion


    }
}
